<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <link rel="stylesheet" type="text/css" href="blogStyles.css"/>
        <title>PDF list</title>
    </head>
    <body>
        <header>CollabBlog</header>
        <br>
        <br>
        <br>
        <?php
// Connect to the database
$dbLink = new mysqli('localhost', 'root', "", 'db_name');
if(mysqli_connect_errno()) {$dbLink = new mysqli('localhost', 'root', "", 'db_name');
    die("MySQL connection failed: ".mysqli_connect_error());
}
// Query for a list of all existing files
$sql = 'SELECT `id`, `file_name`, `type`, `size`, `date` FROM `pdfs`
    ORDER BY `id` DESC';
$result = $dbLink->query($sql);
// Check if it was successfull
if($result) {
    
    // Make sure there are some files in the database
    if($result->num_rows == 0) {
        echo '<p>There are no files in the database</p>';
    }
    else {
        
        // Print the top of a table
        echo '<table width="100%">
                <tr>
                    <td><b>Name</b></td>
                    <td><b>type</b></td>
                    <td><b>Size (bytes)</b></td>
                    <td><b>Created</b></td>
                    <td><b>Download</b></td>
                    <td><b>View</b></td>
                    
                </tr>';
        // Print each file
        while($row = $result->fetch_assoc()) {
            echo "
                <tr>
                    <td>{$row['file_name']}</td>
                    <td>{$row['type']}</td>
                    <td>{$row['size']}</td>
                    <td>{$row['date']}</td>
                    <td><a href='download.php?id={$row['id']}'>Download</a></td> 
                    <td><a target='_blank' href='viewfile.php?id={$row['id']}'>View</a></td>
                    
                    </tr>"; 
        
                    
        }
        // Close table
        echo '</table>';
        
    }
    // Free the result
    $result->free();
}
else
{
    // if connetion to database failed
    echo 'Error! SQL query failed:';
    echo "<pre>{$dbLink->error}</pre>";
}
// Close the mysql connection
$dbLink->close();
//  link to return back to the main page
echo '<p>Click <a href="newfunctions.php">here</a> to go back</p>';
?>         
    </body>
</html>
